grammar Value;

tokens {
	NULL = 'null';
	TRUE = 'true';
	FALSE = 'false';
}

@rulecatch {
}

@header{
	package ac.jp.u_tokyo.SyncLib.valueParser;
	import java.util.LinkedList;
	import java.util.List;
	import java.util.Map;
	import java.util.HashMap;
}

@members {
	public static Object[] parse(String text) throws RecognitionException {
		ANTLRStringStream input = new ANTLRStringStream(text);
		ValueLexer lexer = new ValueLexer(input);
		ValueParser parser = new ValueParser(new CommonTokenStream(lexer));
		return parser.prims().toArray(new Object[] {});
	}
}

@lexer::header{
package ac.jp.u_tokyo.SyncLib.valueParser;
}

fragment DIGIT 
	:	'0'..'9';
	
fragment ULETTER
	:	'A'..'Z';
fragment LLETTER
	:	'a'..'z'| '*';
fragment LETTER
	:	ULETTER | LLETTER | '_';	

NUMBER : DIGIT+;
NAME 	:	LETTER (DIGIT | LETTER)*;
STRING : ('\"' | '\'') (~('\"' | '\''))* ('\"' | '\'');


WS
    : (' '|'\t' | '\n')+ {skip();}
    ; 
 
prims returns [List<Object> result]
	: {$result = new LinkedList();}
	(prim {$result.add($prim.result);}	','?)+;   
prim returns [Object result]
	: NAME {$result = $NAME.text;} 
	| STRING {$result = $STRING.text.substring(1, $STRING.text.length() - 1);}
	| NUMBER {$result = new Integer($NUMBER.text);}
	| NULL {$result = null;}
	| TRUE {$result = true;}
	| FALSE {$result = false;}
	| dict {$result = $dict.result;};
dict returns [Map result]
	: {$result  = new HashMap();}
		'{' (p1=prim ('=' | '->') p2=prim { $result.put($p1.result, $p2.result);} ','?)* '}';
 